package com.a2j.beans.base;

import com.a2j.beans.base.code.ResponseCode;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * Author: bzb
 * Data: 2021/8/10 19:09
 * Desc: 基类
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@ApiModel(description = "最外层Base包装类")
public class BaseBean<T> implements Serializable {

    @ApiModelProperty(value = "返回实体类")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private T data;

    @ApiModelProperty(value = "响应码")
    private int code;

    @ApiModelProperty(value = "请求状态message")
    private String msg;

    @ApiModelProperty(value = "请求是否成功")
    private boolean success;

    // 成功请求
    public static <T> BaseBean<T> success(T t) {
        BaseBean<T> baseBean = new BaseBean<>();
        baseBean.setData(t);
        baseBean.setCode(ResponseCode.SUCCESS.getCode());
        baseBean.setSuccess(true);
        baseBean.setMsg(ResponseCode.SUCCESS.getMessage());
        return baseBean;
    }

    // 成功请求
    public static <T> BaseBean<T> success(ResponseCode responseCode) {
        BaseBean<T> baseBean = new BaseBean<>();
        baseBean.setData(null);
        baseBean.setSuccess(true);
        baseBean.setCode(ResponseCode.SUCCESS.getCode());
        baseBean.setMsg(ResponseCode.SUCCESS.getMessage());
        return baseBean;
    }

    public static <T> BaseBean<T> failed() {
        return failed(ResponseCode.FAILED);
    }

    public static <T> BaseBean<T> failed(String msg) {
        return failed(ResponseCode.FAILED.getCode(), msg);
    }

    // 失败请求
    public static <T> BaseBean<T> failed(ResponseCode responseCode) {
        return failed(responseCode.getCode(), responseCode.getMessage());
    }

    // 失败请求
    public static <T> BaseBean<T> failed(int code, String msg) {
        BaseBean<T> baseBean = new BaseBean<>();
        baseBean.setData(null);
        baseBean.setMsg(msg);
        baseBean.setCode(code);
        baseBean.setSuccess(false);
        return baseBean;
    }
}
